package com.meiya.baseweb.action;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.dispatcher.Dispatcher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.mockrunner.mock.web.MockServletContext;
import com.opensymphony.xwork2.ActionProxy;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:dataSourceContext.xml")
public class PubopionActionTestb {
	
	@Test
	public void testx() throws Exception{
		/*
		** Create a Dispatcher.
		** This is an expensive operation as it has to load all
		** the struts configuration so you will want to reuse the Dispatcher for
		** multiple tests instead of re-creating it each time.
		**
		** In this example I'm setting configuration parameter to override the
		** values in struts.xml.
		*/
		  HashMap<String, String> params = new HashMap<String, String>();
		  // Override struts.xml config constants to use a guice test module
//		  params.put("struts.objectFactory", "spring");
//		  params.put("guice.module", "test.MyModule");

		  MockServletContext servletContext = new MockServletContext();
		  Dispatcher dispatcher = StrutsTestContext.prepareDispatcher(
		       servletContext, params);

		/*
		**  Create an ActionProxy based on the namespace and action.
		**  Pass in request parameters and session attributes needed for this
		**  test.
		*/
		  StrutsTestContext context = new StrutsTestContext(
		      dispatcher, servletContext);
		  Map<String, String> requestParams = new HashMap<String, String>();
		  Map<String, Object> sessionAttributes = new HashMap<String, Object>();
		  requestParams.put("username", "test");
		  requestParams.put("password", "test");

		  ActionProxy proxy = context.createActionProxy(
		      "/admin",      // namespace
		      "LoginSubmit", // Action
		      "", // method
		      requestParams,
		      sessionAttributes);

		  assertTrue(proxy.getAction() instanceof PubopionAction);

		  // Get the Action object from the proxy
		  PubopionAction action = (PubopionAction) proxy.getAction();

		  // Inject any mock objects or set any action properties needed
//		  action.setXXX(new MockXXX());

		  // Run the Struts Interceptor stack and the Action
		  String result = proxy.execute();

		  // Check the results
		  assertEquals("success", result);

		  // Check the user was redirected as expected
		  assertEquals(true, context.getMockResponse().wasRedirectSent());
		  assertEquals("/admin/WelcomeUser.action",
		      context.getMockResponse().getHeader("Location"));

		  // Check the session Login object was set
		  assertEquals(true,
		      context.getMockRequest().getSession().getAttribute(
		         "Constants.SESSION_LOGIN"));
	}
}
